<template>
    <div class="p-2">
        <el-card shadow="never">
            <template #header>
                <el-row justify="space-between">
                    <el-col :span="1.5">
                        <el-form :model="queryParams" ref="queryFormRef" :inline="true">
                            <el-form-item label="申请日期：" prop="timeDate">
                                <el-date-picker style="width: 200px;" v-model="queryParams.timeDate" type="daterange"
                                    range-separator="至" start-placeholder="开始时间" end-placeholder="结束时间"
                                    @change="handleTimeChange" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
                            </el-form-item>
                            <el-form-item label="模糊查询：" prop="workName">
                                <el-input style="width: 100px;" v-model="queryParams.workName"
                                    placeholder="请输入"></el-input>
                            </el-form-item>
                            <el-form-item label="供应商：" prop="workName">
                                <el-input style="width: 100px;" v-model="queryParams.workName"
                                    placeholder="请输入"></el-input>
                            </el-form-item>
                            <el-form-item label="入库：" prop="orderPrintStatus">
                                <el-select style="width: 100px;" v-model="queryParams.orderPrintStatus" clearable>
                                    <el-option label="未打印" value="0"></el-option>
                                    <el-option label="已打印" value="1"></el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="对账状态：" prop="orderPrintStatus">
                                <el-select style="width: 100px;" v-model="queryParams.orderPrintStatus" clearable>
                                    <el-option label="未打印" value="0"></el-option>
                                    <el-option label="已打印" value="1"></el-option>
                                </el-select>
                            </el-form-item>

                            <el-form-item>
                                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                            </el-form-item>
                        </el-form>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button @click="exportPurchaseList" icon="DocumentDelete" type="success">下载EXCEL</el-button>
                    </el-col>
                </el-row>
            </template>
            <el-table v-loading="loading" :data="dataList" stripe>
                <el-table-column label="所属公司" prop="companyName" align="center"></el-table-column>
                <el-table-column label="申请人" prop="createByName" align="center"></el-table-column>
                <el-table-column label="申请日期" prop="createTime" align="center"></el-table-column>
                <el-table-column label="交货日期" prop="needArriveDate" align="center"></el-table-column>
                <el-table-column label="采购单号" prop="paperPurchaseCode" align="center"></el-table-column>
                <el-table-column label="纸张大类" prop="customizedName" align="center"></el-table-column>
                <el-table-column label="材料小类" prop="paperTypeName" align="center"></el-table-column>
                <el-table-column label="规格" prop="specifications" align="center"></el-table-column>
                <el-table-column label="品名" prop="specsDetail" align="center"></el-table-column>
                <el-table-column label="单位" prop="unit" align="center"></el-table-column>
                <el-table-column label="单价" prop="singlePrice" align="center"></el-table-column>
                <el-table-column label="采购数量" prop="applyPurchaseNumber" align="center"></el-table-column>
                <el-table-column label="入库数量" prop="inNumber" align="center"></el-table-column>
                <el-table-column label="入库金额" prop="inAmount" align="center"></el-table-column>
                <el-table-column label="补偿金额" prop="compensateAmount" align="center"></el-table-column>
                <el-table-column label="供应商" prop="supplyName" align="center"></el-table-column>
                <el-table-column label="关联订单号" prop="customizedName" align="center"></el-table-column>
                <el-table-column label="审批" prop="updateTime" align="center">
                    <template #default="scope">
                        <span v-if="scope.row.approvalStatus === '0'" style="color: #DF9BF9;">未审批</span>
                        <span v-if="scope.row.approvalStatus === '1'" style="color: #72C64A;">通过</span>
                        <span v-if="scope.row.approvalStatus === '2'" style="color: #F76F62;">拒绝</span>
                    </template>
                </el-table-column>
                <el-table-column label="入库状态" prop="updateTime" align="center">
                    <template #default="scope">
                        <span v-if="scope.row.inStatus === '0'" style="color: #F75F51;">未入库</span>
                        <span v-if="scope.row.inStatus === '1'" style="color: #E6A33F;">部分入库</span>
                        <span v-if="scope.row.inStatus === '2'" style="color: #72C64A;">已入库</span>
                    </template>
                </el-table-column>
                <el-table-column label="对账状态" prop="updateTime" align="center">
                    <template #default="scope">
                        <span v-if="scope.row.reconciliationStatus === '0'" style="color: #DF9BF9; ">未对账</span>
                        <span v-if="scope.row.reconciliationStatus === '1'" style="color: #69AFF0; ">已对账</span>
                        <span v-if="scope.row.reconciliationStatus === '2'" style="color: #72C64A; ">已付款</span>
                    </template>
                </el-table-column>
                <el-table-column label="编辑状态" prop="updateTime" align="center">
                    <template #default="scope">
                        <span v-if="scope.row.editStatus === '0'" style="color: #DF9BF9;">未编辑</span>
                        <span v-if="scope.row.editStatus === '1'" style="color: #72C64A;">已编辑</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" fixed="right" align="center" width="160px">
                    <template #default="scope">
                        <el-row>
                            <el-col :span="24">
                                <el-tooltip content="编辑">
                                    <el-button @click="handleOpenEditClick(scope.row)" link
                                        :style="{ fontSize: '15px' }" type="primary" icon="EditPen"></el-button>
                                </el-tooltip>
                                <el-tooltip content="审批" placement="top">
                                    <el-button @click="handleOpenApprovalClick(scope.row)" link
                                        :style="{ fontSize: '15px' }" type="success" icon="Coordinate"></el-button>
                                </el-tooltip>
                                <el-tooltip content="删除">
                                    <el-button @click="handleDelete(scope.row, getList)" link :style="{ fontSize: '13px' }"
                                        type="danger" icon="CircleClose"></el-button>
                                </el-tooltip>
                                <el-tooltip content="采购入库">
                                    <el-button @click="handlePurchase(scope.row)" link :style="{ fontSize: '13px' }"
                                        type="primary" icon="CopyDocument"></el-button>
                                </el-tooltip>
                            </el-col>
                            <el-col :span="24">
                                <el-tooltip content="对账" placement="top">
                                    <el-button @click="handleOpenReconciliation(scope.row)" link
                                        :style="{ fontSize: '13px' }" type="warning" icon="Money"></el-button>
                                </el-tooltip>
                                <el-tooltip content="撤销" placement="top">
                                    <el-button @click="handleRevoke(scope.row, getList)" link
                                        :style="{ fontSize: '13px' }" type="danger" icon="RefreshRight"></el-button>
                                </el-tooltip>
                                <el-tooltip content="详情">
                                    <el-button @click="handleOpenPurchaseApplyDetails(scope.row)" link
                                        :style="{ fontSize: '13px' }" type="primary" icon="Document"></el-button>
                                </el-tooltip>
                            </el-col>
                        </el-row>
                    </template>
                </el-table-column>

            </el-table>
            <pagination v-show="totalData > 0" :total="totalData" v-model:page="queryParams.pageNum"
                v-model:limit="queryParams.pageSize" @pagination="getList" />
        </el-card>

        <!-- 编辑采购单 -->
        <EditPurchaseOrder :visible="editVisible" :initList="editObj" :suppliersList="suppliersList" @submit="getList"
            @close="editVisible = false" />
        <!-- 采购入库 -->
        <PurchaseReceipt :visible="purchaseVisible" :initList="purchaseRow" :unitEnumeration="unit_enumeration"
            @submit="getList" @close="purchaseVisible = false" />
        <!-- 审批 -->
        <Approval :visible="approvalVisible" :initList="approvalObj" @submit="getList"
            @close="approvalVisible = false" />
        <!-- 详情 -->
        <WarehousingDetails :visible="detailVisible" :initList="detailRow" :detailList="detailList"
            :unitEnumeration="unit_enumeration" @close="detailVisible = false" />
        <!-- 撤销 -->
        <Revoke :visible="revokeVisible" @close="revokeVisible = false" />
        <!-- 对账 -->
        <Reconciliation :visible="reconciliationVisible" :initList="reconciliationRow" @submit="getList"
            @close="reconciliationVisible = false" />
    </div>
</template>
<script setup lang="ts">
import { type FormInstance } from 'element-plus';
import { getPaperPurchaseOrderListApi } from '@/api/sj/purchaseManagement/paperProcurement/index';
import { getSuppliersApi } from '@/api/sj/purchaseManagement/supplierManagement';
import { getFormattedTimestamp } from '@/utils/dateFormat';


/** 编辑 */
import EditPurchaseOrder from './components/editPurchaseOrder.vue';
import useEditPurchaseOrder from '@/hooks/purchaseManagement/paperProcurement/useEditPurchaseOrder';

/** 采购入库 */
import PurchaseReceipt from './components/purchaseReceipt.vue';
import usePurchaseReceipt from '@/hooks/purchaseManagement/toBeStored/usePurchaseReceipt';

/** 审批 */
import Approval from './components/approval.vue';
import useApproval from '@/hooks/purchaseManagement/paperProcurement/useApproval';

/** 详情 */
import WarehousingDetails from './components/warehousingDetails.vue';
import useWarehousingDetails from '@/hooks/purchaseManagement/purchaseOrder/useWarehousingDetails';


/** 撤销 */
import Revoke from './components/revoke.vue';

/** 对账 */
import Reconciliation from './components/reconciliation.vue';
import useReconciliation from '@/hooks/purchaseManagement/paperProcurement/useReconciliation';

const { editVisible, revokeVisible, editObj, handleOpenEditClick, handleDelete } = useEditPurchaseOrder();
const { purchaseVisible, purchaseRow, handlePurchase } = usePurchaseReceipt();
const { approvalVisible, approvalObj, handleOpenApprovalClick } = useApproval();
const { detailVisible, detailRow,
    detailList, handleOpenPurchaseApplyDetails } = useWarehousingDetails();

const { reconciliationVisible, reconciliationRow, handleOpenReconciliation, handleRevoke } = useReconciliation();

const { proxy } = getCurrentInstance() as any;

const { unit_enumeration } = toRefs(proxy?.useDict('unit_enumeration'));

const queryFormRef = ref<FormInstance>();

const data = reactive<pageDataOnlyQuery<any>>({
    queryParams: {
        timeDate: '',
        startDate: '',
        endDate: '',
        pageSize: 10,
        pageNum: 1
    }
});

const { queryParams } = toRefs(data);

const loading = ref(false);
const dataList = ref<any>([]);
const totalData = ref(0);
const suppliersList = ref([]);

const handleQuery = () => {
    queryParams.value.pageNum = 1;
    getList();
};

const resetQuery = () => {
    queryFormRef.value?.resetFields();
    queryParams.value.startDate = '';
    queryParams.value.endDate = '';
    queryParams.value.timeDate = '';
    handleQuery();
};

/** 日期选择 */
const handleTimeChange = (val: any) => {
    queryParams.value.startDate = val[0];
    queryParams.value.endDate = val[1];
};

const getList = async () => {
    try {
        const { rows, total } = await getPaperPurchaseOrderListApi(queryParams.value);
        dataList.value = rows;
        totalData.value = total;
    } catch (error) {

    }
};


/** 获取全部供应商列表 */
const getSuppliers = async () => {
    const { code, data } = await getSuppliersApi('1');
    if (code === 200) {
        suppliersList.value = data;
    }
};

const exportPurchaseList = () => {
    proxy?.download('/system/paperPurchaseOrder/export', { ...queryParams.value }, `纸张采购单${getFormattedTimestamp()}.xlsx`);
};

onMounted(() => {
    getList();
    getSuppliers();
});

</script>
<style lang="scss" scoped>
.red {
    color: red;
}

.orange {
    color: orange;
}

.green {
    color: green;
}

.purple {
    color: #DF9BF9;
}
</style>